package cn.xxstudy.visitshop.widget


import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.util.AttributeSet
import android.view.View
import cn.xxstudy.visitshop.R
import cn.xxstudy.visitshop.util.dp2Px


/**
 * @author ：DELL on 2021/3/23 .
 * @packages ：cn.xxstudy.visitshop.widget .
 * TODO:ViewPager指示器
 */
enum class Indicator {
    DOT,
    ROUND,
    BLOCK
}


class ViewPagerIndicator : View {

    private val mPaint = Paint(Paint.ANTI_ALIAS_FLAG)
    private var defaultColor = Color.parseColor("#C0C0C0")
    private var selectedColor = Color.parseColor("#3caee2")
    private val viewWidth = 100f.dp2Px()
    private val viewHeight = 20f.dp2Px()
    private val radius = 8f.dp2Px()
    private var count = 4
    private var indicator = Indicator.DOT

    //单个宽度
    private val blockWidth = viewWidth / count

    //当前选中
    var selectedIndex = 0
        set(value) {
            field = value
            invalidate()
        }

    constructor(context: Context) : this(context, null)
    constructor(context: Context, attributeSet: AttributeSet?) : this(context, attributeSet, 0)
    constructor(context: Context, attributeSet: AttributeSet?, defStyle: Int) : super(
        context,
        attributeSet,
        defStyle
    ) {
        mPaint.color = defaultColor
        mPaint.style = Paint.Style.FILL
        val typeArray = context.obtainStyledAttributes(attributeSet, R.styleable.ViewPagerIndicator)
        defaultColor =
            typeArray.getColor(R.styleable.ViewPagerIndicator_default_color, defaultColor)
        selectedColor =
            typeArray.getColor(R.styleable.ViewPagerIndicator_selected_color, selectedColor)
        val indicatorInt = typeArray.getInt(R.styleable.ViewPagerIndicator_indicator_type, 0)
        indicator = when (indicatorInt) {
            0 -> Indicator.DOT
            1 -> Indicator.ROUND
            else -> Indicator.BLOCK
        }
        typeArray.recycle()
    }


    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        setMeasuredDimension(viewWidth.toInt(), viewHeight.toInt())
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        when (indicator) {
            Indicator.DOT -> drawDotIndicator(canvas)
            else -> drawIndicator(canvas, indicator)
        }
    }


    private fun drawDotIndicator(canvas: Canvas) {
        var startX = blockWidth / 2
        val currentCy = viewHeight / 2
        for (i in 0 until count) {
            mPaint.color = if (i == selectedIndex) selectedColor else defaultColor
            canvas.drawCircle(startX, currentCy, radius, mPaint)
            startX += blockWidth
        }
    }


    private fun drawIndicator(canvas: Canvas, indicator: Indicator) {
        //定义radius为rect高度，宽度为radius的2.5倍
        val roundRectWidth = radius * 2.5f
        var left = (blockWidth - roundRectWidth) / 2f
        val top = (viewHeight - radius) / 2f
        var right = left + roundRectWidth
        val bottom = viewHeight / 2 + radius / 2f
        for (i in 0 until count) {
            mPaint.color = if (i == selectedIndex) selectedColor else defaultColor
            if (indicator == Indicator.ROUND) {
                canvas.drawRoundRect(left, top, right, bottom, radius / 2f, radius / 2f, mPaint)
            } else {
                canvas.drawRect(left, top, right, bottom, mPaint)
            }
            left = blockWidth * (i + 1) + (blockWidth - roundRectWidth) / 2f
            right = left + roundRectWidth
        }
    }


    fun setConfig(count: Int, indicator: Indicator) {
        this.count = count
        this.indicator = indicator
        invalidate()
    }
}